// pages/outvisit/approval/details.js
var api = require("../../../utils/api.js");
var common = require("../../../utils/common.js");
const taskDetailsTitles = common.taskDetailsTitles;//详情字段
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,//任务ID
    collectionCaseId: 0,//催收案件ID
    taskDetailsIsHide: true,//任务详情是否隐藏
    taskDetailsList: [],//任务详情列表
    causeState: 1,//外访信息状态
    cause: {//外访信息
      externalReasons: "",
      externalRequest: "",
      externalAccessList: ""
    },
    stopOutvisitFormIsHide: true,//撤销外访表单是否隐藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    // 设置任务ID
    that.setData({
      id: id,
    }, () => {
      // 获取任务详情
      that.getTaskDetails();
    });
  },

  // 获取任务详情
  getTaskDetails: function () {
    var that = this;
    wx.showLoading({
      title: "加载中",
      mask: true
    });//显示加载中
    api.getTaskDetails(that.data.id).then(res => {
      if (res.code == 0) {
        wx.hideLoading();
        var data = res.data;
        // 获取催收案件ID
        var collectionCaseId = data.collectionCaseId;
        // 获取任务详情列表
        var taskDetailsList = [];
        for (var field in taskDetailsTitles) {
          if (data[field] !== undefined && data[field] !== "") {
            taskDetailsList.push({
              title: taskDetailsTitles[field],
              value: data[field]
            });
          }
        }
        // 获取外访信息
        var cause = {};
        cause.externalReasons = data.externalReasons !== undefined ? data.externalReasons : "";
        cause.externalRequest = data.externalRequest !== undefined ? data.externalRequest : "";
        cause.externalAccessList = data.externalAccessList !== undefined ? data.externalAccessList : "";

        // 更新数据
        that.setData({
          collectionCaseId: collectionCaseId,
          taskDetailsList: taskDetailsList,
          cause: cause
        });
      }
    });
  },

  // 详情按钮点击事件
  detailsBtnClick: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.id;
    var url = "";
    switch (type) {
      case '1':
        url = "../task/collectionRecord";
        break;
      case '2':
        url = "../task/totalDebtDetails";
        break;
      default:
        break;
    }
    wx.navigateTo({
      url: url + '?id=' + that.data.collectionCaseId
    });
  },

  // 同意外访
  consent: function () {
    var that = this;
    wx.navigateTo({
      url: "consent?id=" + that.data.id
    });
  },

  // 撤销外访
  cancelOut: function (e) {
    var that = this;
    var cause = e.detail.value.cause;
    if (!cause.trim()) {
      wx.showToast({
        title: "撤销原因不能为空",
        icon: "none"
      });
      return;
    }
    api.cancelOut(that.data.id, cause);
  },

  // 撤销外访表单显示与隐藏
  stopOutvisitFormShowOrHide: function () {
    var stopOutvisitFormIsHide = this.data.stopOutvisitFormIsHide;
    if (stopOutvisitFormIsHide) {
      stopOutvisitFormIsHide = false;
    } else {
      stopOutvisitFormIsHide = true;
    }
    this.setData({
      stopOutvisitFormIsHide: stopOutvisitFormIsHide
    })
  },

  // 外访信息切换
  causeTabClick: function (e) {
    this.setData({
      causeState: e.currentTarget.dataset.id
    });
  },

  // 任务详情收起
  packUpTask: function () {
    var that = this;
    this.setData({
      taskDetailsIsHide: that.data.taskDetailsIsHide ? false : true
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})